package processs;

import example.demo_navi.R;
import gui_View.Playlistview;

import java.util.ArrayList;

import models.Listvid;
import utility.DbConnect;
import videos.Main2;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import clicks.Listvideoclick;
import clicks.OnLoadMoreListener;

public class NaviDraw extends Fragment{

	public static final String ARG_NUMBER="position";
	private Playlistview pl;
	private DbConnect dbc;
	private String lida;
	private ArrayList<Listvid> al;
	private int off=0;
	public NaviDraw(){
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		dbc=new DbConnect(getActivity().getApplicationContext());
		dbc.opend();
		al=new ArrayList<Listvid>();
		View v=inflater.inflate(R.layout.activity_main, container, false);
		pl=(Playlistview) v.findViewById(R.id.videosListView);
		pl.setonPlistvi(new xuli());
	//	int i=getArguments().getInt(ARG_NUMBER);
	//	String s=getArguments().getString("chara");
		String[] mni=getResources().getStringArray(R.array.Cate_video);
	//	getActivity().getActionBar().setTitle(mni[i]);
	//	Getyou2 ge=new Getyou2(responseHandler);
		new Getlat().execute();
		pl.setOnLoadMoreListener(new OnLoadMoreListener() {
			
			public void onLoadMore() {
				// TODO Auto-generated method stub
				new getlat2().execute();
			}
		});
		
    	
		return v;
	}
	
/*	Handler responseHandler = new Handler() {
		public void handleMessage(Message msg) {
			populateListWithVideos(msg);
		};
	};

	private void populateListWithVideos(Message msg) {
		
		Lib2 lib = (Lib2) msg.getData().get(Getyou2.Listvid);
		pl.setPlaylist(lib.getPlayli());
		for(Listvid i:lib.getPlayli()){
			dbc.insert_playlist(new Listvid(i.getLid(),i.getLiti(),i.getPdate(), i.getLithum(), i.getCat_id()));
		Log.d("LayLan",i.getLiti()+ "- " +i.getCat_id());}
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		responseHandler=null;
		super.onStop();
	}  */

	private class Getlat extends AsyncTask<Void, Void, Void>{

		ProgressDialog pd;
		
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			al=dbc.getAllPlaylist(0,20);
			for(int i=0;i<al.size();i++){
				Listvid vi=al.get(i);
				lida=vi.getLid();
				String titi=vi.getLiti();
				String thumb=vi.getLithum();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			pd.dismiss();
			pl.setPlaylist(al);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pd=new ProgressDialog(getActivity());
			pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pd.setMessage("Loading Playlists...");
			pd.show();
		}
		
	}
	
	private class getlat2 extends AsyncTask<Void, Void, Void>{

		ArrayList<Listvid> al2=new ArrayList<Listvid>();
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			off+=10;
			al2=dbc.getAllPlaylist(off,10);
			for(int i=0;i<al.size();i++){
				Listvid vi=al.get(i);
				lida=vi.getLid();
				String titi=vi.getLiti();
				String thumb=vi.getLithum();
			}
			al.addAll(al2);
			Log.d("SizeF4",""+al.size());
			return null;
		}

		@Override
		protected void onCancelled(Void result) {
			// TODO Auto-generated method stub
			pl.onLoadMoreComplete();
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			int cur=pl.getFirstVisiblePosition();
			pl.setPlaylist(al);
			pl.setSelectionFromTop(cur+1, 0);
			pl.onLoadMoreComplete();
			super.onPostExecute(result);
		}
		
	}
	
	private class xuli implements Listvideoclick{

		public void onListvidClicked(Listvid lv) {
			// TODO Auto-generated method stub
			Intent in=new Intent(getActivity().getApplicationContext(), Main2.class);
			in.setData(Uri.parse(lv.getLid()));
		//	in.putExtra("holle", lv.getLid());
			Log.d("tester", lv.getLid());
			startActivity(in);
		}		
	}
}
